/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*************************************************************************
 * File: testclient.cpp
 *************************************************************************
 *\author: starlove
 *************************************************************************/

#include "testpeer.h"

CFixedAllocator<CTestPeer> g_peer_allocator;
CTestPeer::CTestPeer(ISocket *pSocket, SOCKET sck /* = INVALID_SOCKET */)
{
    m_pSocket = pSocket;
    m_raw_sck = sck;
}

CTestPeer::~CTestPeer()
{
}

void CTestPeer::OnReadReady(LPPACKET pPacket)
{
    //printf(">>> Socket %d recv data: %d\n", m_raw_sck, m_recvbuffer.GetDataSize());
}

void CTestPeer::OnError()
{
    //printf("\n>>> Socket %d has error. the socket is closed\n", m_raw_sck);
    Destory();
    g_peer_allocator.free(this);
}

void CTestPeer::ProcessHttpRequest(CMapWrapper *__request, LPHTTPRESPONSE __response, LPNETWORKBUFFER pOutBuff)
{
    CMapData request;
    request.Load(__request->GetBuffer());

    strcpy(__response->status_code,"404 Not Found");
    pOutBuff->AppendStr("Not Found!");

    printf(">>> has request: HTTP_METHOD: %s, HTTP_URL: %s, HTTP_QUERYSTRING: %s, HTTP_VER: HTTP/1.%d\n, HTTP_FORM_DATA: %s\n"
           ,request.GetString<ENCODESTR>("HTTP_METHOD")
           ,request.GetString<ENCODESTR>("HTTP_URL")
           ,request.GetString<ENCODESTR>("HTTP_QUERYSTRING")
           ,request.GetNumber<ENCODESTR, int>("HTTP_VER")
           ,request.GetString<ENCODESTR>("HTTP_FORM_DATA")
          );
}
